'use strict'
import { Transform } from 'stream'
export default function DataFormater(req, res) {
  const transform = Transform()
  let count = 0
  let dataBuffer = Buffer.from('')
  transform._transform = function (buffer, enc, next) {
    dataBuffer = Buffer.concat([dataBuffer, buffer])
    if (dataBuffer.toString().length === Number(req.headers['content-length'])) {
      this.push('over!!')
    }
    /* if (count === 0) {
      const boundaryStr = bufferStr.split('\r')[0]
      let myFormData = `${boundaryStr}
Content-Disposition: form-data; name="c"

3
`
      this.push(myFormData)
      // process.stdout.write(boundaryStr)
    } */
    // this.push(bufferStr)
    count++
    next()
  }

  transform.on('data', buf => {

    // process.stdout.write('data:' + buf.toString() + '\n')
  })

  transform.on('end', () => {
    // process.stdout.write(dataStr)
    count = 0
    dataStr = ''
    process.stdout.write('end' + '\n')
    res.write('aaaaa')
    res.write('bbbbbbbbbb')
    res.end()
  })
  transform.on('finish', () => {
    process.stdout.write('finish' + '\n')
  })
  // transform.end()
  return transform
}
